<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\WithdrawService as Withdraw;
use Illuminate\Http\Request;

class WithdrawController extends BaseController
{
    //提现
    protected $fieldMsgs = [
        'name' => ['en' => 'name', 'cn' => '姓名', 'ismust' => true],
        'phone' => ['en' => 'phone', 'cn' => '手机号的', 'ismust' => true],
        'card_no' => ['en' => 'card_no', 'cn' => '银行卡号', 'ismust' => true],
        'bank_name' => ['en' => 'bank_name', 'cn' => '开户行', 'ismust' => true],
        'remark' => ['en' => 'remark', 'cn' => '备注'],
    ];
    public function __construct()
    {
        $this->db = new Withdraw();
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $status = $request->post('status', '');
        $res = $this->db->getList($pages, $uid, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加账户信息
    public function addInfo(Request $request)
    {
        $validate = $this->checkField($request, $this->fieldMsgs);
        if ($validate['code'] < 0) {
            return $this->toJson(-2, $validate['msg']);
        }
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $res = $this->db->addInfo($uid, $validate['data']);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '修改成功');
    }

    //添加账户信息
    public function modifyInfo(Request $request)
    {
        $validate = $this->checkField($request, $this->fieldMsgs);
        if ($validate['code'] < 0) {
            return $this->toJson(-2, $validate['msg']);
        }
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $res = $this->db->modifyInfo($uid, $validate['data']);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '添加成功');
    }

    //详情
    public function getInfo(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $res = $this->db->getInfo($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    public function add(Request $request){
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
        if (!$user) {
            return $this->toJson(-2, '未登录');
        }
        $amount = $request->post('amount', 0);
        if(!$amount || $amount > ($user['income'] - $user['income_freeze'])) {
            return $this->toJson(-2, '请输入正确的提现金额，且要小于可提现金额');
        }
        $res = $this->db->add($user, $amount);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}